package com.zhongchaowuye.controller;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.zhongchaowuye.pojo.B_GZ;
import com.zhongchaowuye.pojo.B_PD_M;
import com.zhongchaowuye.pojo.B_TZh;
import com.zhongchaowuye.service.B_PD_MService;
import com.zhongchaowuye.service.B_TZhService;

@Controller
@RequestMapping("/b_PD_M")
public class B_PD_MController {

	@Resource
	private B_PD_MService b_PD_MService;
	@Resource
	private B_TZhService b_TZhService;
	
	@RequestMapping(value ="/getB_PD_M",method = RequestMethod.POST)
	@ResponseBody
	//根据id获取B_PD_M对象信息
	public Map<String,Object> getB_PD_M(HttpServletRequest request,B_PD_M pd){
		Map<String,Object> m=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		try {
			B_PD_M b_PD_M=b_PD_MService.getB_PD_M(pd);
			reMap.put("b_PD_M", b_PD_M);
			reMap.put("status", "success");
			reMap.put("message", "OK");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}

	@RequestMapping(value ="/getB_PD_MList",method = RequestMethod.POST)
	@ResponseBody
	//获取列表
	public Map<String,Object> getB_PD_MList(HttpServletRequest request,B_PD_M b_PD_M){
		Map<String,Object> reMap=new HashMap<>();//定义返回值，其中有一项必须是集合，先按照下面的写法
		try {
			//List<Map<String,Object>> rList=b_PD_MService.getB_PD_MList(b_PD_M); //不带分页（注意实现类中的相应写法）
			Page<Map<String,Object>> rList = (Page)b_PD_MService.getB_PD_MList(b_PD_M);
			
			//System.out.println(rList);
			reMap.put("rows", rList);
			reMap.put("total", rList.getTotal());
			reMap.put("pageIndex", b_PD_M.getPageIndex());
			reMap.put("pageSize", b_PD_M.getPageSize());
			reMap.put("status", "success");
			reMap.put("message", "OK");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}

	@RequestMapping(value ="/insertB_PD_M",method = RequestMethod.POST)
	@ResponseBody
	//添加
	public Map<String,Object> insertB_PD_M(HttpServletRequest request,B_PD_M b_PD_M)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		try 
		{
			//补齐创建者与创建时间
			//b_PD_M.setCrea("");
			//Timestamp cur=new Timestamp(System.currentTimeMillis()); 
			//b_PD_M.setCrea_time(cur); //或者直接在sql语句中使用now()函数填充对应的值
			int flag=b_PD_MService.insertB_PD_M(b_PD_M);
			//int flag = 0;
			if(flag>0)
			{
				reMap.put("status", "success");
				reMap.put("message", "OK");
			}
			else
			{
				reMap.put("status", "error");
				reMap.put("message", "ERROR");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}
	
	@RequestMapping(value ="/updateB_PD_M",method = RequestMethod.POST)
	@ResponseBody
	//修改
	public Map<String,Object> updateB_PD_M(HttpServletRequest request,B_PD_M b_PD_M)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		try 
		{
			int flag=b_PD_MService.updateB_PD_M(b_PD_M);
			if(flag>0)
			{
				reMap.put("status", "success");
				reMap.put("message", "OK");
			}
			else
			{
				reMap.put("status", "error");
				reMap.put("message", "ERROR");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}
	
	@RequestMapping(value ="/delB_PD_M",method = RequestMethod.POST)
	@ResponseBody
	//软删除
	public Map<String,Object> delB_PD_M(HttpServletRequest request,B_PD_M b_PD_M)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		Map<String,Object> m=new HashMap<>();
		try 
		{
			int flag=b_PD_MService.delB_PD_M(b_PD_M);
			if(flag>0)
			{
				reMap.put("status", "success");
				reMap.put("message", "OK");
			}
			else
			{
				reMap.put("status", "error");
				reMap.put("message", "ERROR");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}
	
	@RequestMapping(value ="/getDjxx",method = RequestMethod.POST)
	@ResponseBody
	//根据指定的类别指定的食（物）品名称获取最近一次的进货价格数据
	public Map<String,Object> getDjxx(HttpServletRequest request,B_TZh b_TZh){
		Map<String,Object> reMap=new HashMap<>();//定义返回值
		try {
			List<Map<String,Object>> rList=b_TZhService.getDjxx(b_TZh); //取单价信息
			//System.out.println(rList);//这种list方式的查询如果没有结果的话，会是[null]的方式返回
			if(rList.contains(null)){
				reMap.put("pd_danjia", 0);
			}else{
				int i=0;
				for(Map m:rList){ //1条统计记录
					if(i==1){
						break;
					}
					System.out.println("取最近价格数据=="+m);
					//double pd_danjia=m.get("pd_danjia")==null ? 0 : ((BigDecimal)m.get("pd_danjia")).doubleValue();
					double pd_danjia=((BigDecimal)m.get("pd_danjia")).doubleValue();
					reMap.put("pd_danjia", pd_danjia);
					i++;
				} //for循环结束
			}
			reMap.put("status", "success");
			reMap.put("message", "OK");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}
}
